package cn.kgc.controller;

import cn.hutool.core.util.StrUtil;
import cn.kgc.base.SystemConstant;
import cn.kgc.entity.Company;
import cn.kgc.feign.UserFeignClient;
import cn.kgc.form.CompanyAddOrReviewForm;
import cn.kgc.form.CompanyAdminReviewForm;
import cn.kgc.form.CompanyHrReviewForm;
import cn.kgc.form.CompanyQueryForm;
import cn.kgc.grace.result.GraceJSONResult;
import cn.kgc.service.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author YC
 */
@RestController
@RequestMapping("/company")
@Api(tags = "公司模块")
@Validated
@Slf4j
public class CompanyController {

    @Resource
    private CompanyService companyService;
    @Resource
    private UserFeignClient userFeignClient;

    @PostMapping("/getCompany")
    @ApiOperation(value = "根据公司名称获取公司信息")
    public GraceJSONResult getCompany(String companyName) {
        return GraceJSONResult.ok(companyService.getCompany(companyName));
    }

    @PostMapping("/settledCompany")
    @ApiOperation(value = "公司申请入驻平台/重新提交审批")
    public GraceJSONResult createCompany(@RequestBody CompanyAddOrReviewForm form) {
        if (StrUtil.isBlank(form.getId())) {
            // 没有信息第一次入驻，新增公司的信息
            return GraceJSONResult.ok(companyService.createCompany(form));
        }
        // 有信息则为审批失败后的重新申请，更新信息（清空上一次的审批建议）
        return GraceJSONResult.ok(companyService.reviewCompany(form));
    }

    @PostMapping("/app/reviewCompanyHR")
    @ApiOperation(value = "用户申请成为公司的HR")
    public GraceJSONResult goReviewCompany(@RequestBody CompanyHrReviewForm form) {
        return GraceJSONResult.ok(companyService.reviewCompanyHr(form));
    }

    @PostMapping("/getInformation")
    @ApiOperation(value = "根据用户ID查询对应公司ID（如果是HR，不是HR返回空字符串）")
    public GraceJSONResult getInformation(String userId) {
        String companyId = (String) userFeignClient.getCompanyIdByUserId(userId).getData();
        if (StrUtil.isBlank(companyId)) {
            return GraceJSONResult.errorMsg("您还没有申请成为公司HR");
        }
        return GraceJSONResult.ok(companyService.getById(companyId));
    }

    @PostMapping("/admin/list")
    @ApiOperation(value = "分页查询待审批公司的集合")
    public GraceJSONResult goReviewCompany(@RequestBody CompanyQueryForm form) {
        return GraceJSONResult.ok(companyService.queryCompanyList(form));
    }

    @PostMapping("/admin/detail")
    @ApiOperation(value = "查看申请信息")
    public GraceJSONResult detail(String companyId) {
        return GraceJSONResult.ok(companyService.getById(companyId));
    }

    @PostMapping("/admin/review")
    @ApiOperation(value = "管理员审批公司入驻")
    public GraceJSONResult reviewCompanyHr(@Validated @RequestBody CompanyAdminReviewForm form) {
        // 审核同意：修改审核状态为1、同时修改用户角色和关联的公司信息（业务层Seata-AT）  审核不通过：修改审核状态2
        Company company = companyService.getById(form.getCompanyId());
        if (!company.getReviewStatus().equals(SystemConstant.REVIEW_STATUS_REVIEWING)) {
            return GraceJSONResult.errorMsg("公司的审核状态不合法，请先提交审核");
        }
        return GraceJSONResult.ok(companyService.review(form));
    }

}